<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/14
 * Time: 9:23
 */

class Suppliers_model extends CI_Model
{

    public $limit_rows = 20;


    public function suppliers_option($user_id = 0)
    {
        $where = '';
        if ($user_id) {
            $where = " AND user_id = '{$user_id}'";
        }

        $query = $this->db->from('suppliers')->where("is_delete = 0 ".$where)->get();

        $suppliers = array();
        foreach ($query->result_array() as $row) {
            $suppliers[$row['suppliers_id']] = trim($row['suppliers_name']);
        }

        return $suppliers;
    }

    public function suppliers_options($param = array(), $is_all = 1)
    {
        $this->db->from('suppliers')->select('suppliers_id,suppliers_name,status')->where("is_delete = 0 ");

        if ($param['supp_name']) {
            $this->db->where("suppliers_name like '%{$param['supp_name']}%'");
        }

        if ($is_all != 1) {
            $this->db->limit($this->limit_rows);
        }

        $query = $this->db->get();

        $suppliers = $query->result_array();

        return $suppliers;
    }

    public function get_supp_id($supp_name)
    {
        $query = $this->db->from('suppliers')->where("suppliers_name = '{$supp_name}'")->get();
        $supp = $query->row_array();

        if (!$supp) {
            $supp['suppliers_id'] = $this->set_supp(array(
                'suppliers_name' => $supp_name
            ));
        }

        return $supp['suppliers_id'];
    }

    public function set_supp($data = array(), $param = array())
    {

        $supp_id = 0;
        if ($data) {
            $data['update_time'] = time();
            $data['sp_level'] = intval($data['sp_level']);
            $data['purchase_eid'] = $data['purchase_eid'];
            if ($param) {
                $this->db->where($param);
                $this->db->update('suppliers', $data);
            } else {
                $info = $this->db->from('suppliers')->where(['suppliers_name'=>trim($this->input->post('suppliers_name'))])->get()->row_array();
                if($info){
                    return false;
                }
                $data['add_time'] = $data['update_time'];
                $this->db->insert('suppliers', $data);

                $supp_id = $this->db->insert_id();
            }
        }

        return $supp_id;
    }

    public function suppliers_list($param = array())
    {


        $this->db->select('s.*');
        $this->db->from('suppliers AS s');

        if ($param['supp_name']) {
            $this->db->where("suppliers_name LIKE '%{$param['supp_name']}%'");
        }
        if($param['status'] >0){
            $this->db->where(['status'=>$param['status']]);
        }

        $this->db->limit($this->limit_rows,$this->limit_rows * $param['page']);


        $this->db->order_by('s.suppliers_id DESC');
        $query = $this->db->get();

        $suppliers = array();
        foreach ($query->result_array() as $k => $row) {
            $suppliers[$row['suppliers_id']] = $row;
            $purchase_name = $this->db->from('purchase')->where(['e_id'=>$row['purchase_eid']])->get()->row_array();
            $suppliers[$row['suppliers_id']]['purchase_name'] = $purchase_name['purchase_name'];
        }
        return $suppliers;
    }

    public function suppliers_storage_list($param)
    {
        $suppliers_list = $this->suppliers_list($param);

        $suppliers_storage_list = array();
        $today_start = strtotime('today');
        $today_end = $today_start + 3600 * 24;
        foreach ($suppliers_list as $k => $row) {
            $purchase_query = $this->db->from('goods_sku_storage')->select_sum('purchase_number')->where("supp_id = '{$row['suppliers_id']}'")->get();
            $row += $purchase_query->row_array();

            $storage_query = $this->db->from('goods_sku_storage')->select_sum('storage_number')->where("supp_id = '{$row['suppliers_id']}'")->get();
            $row += $storage_query->row_array();

            $today_storage_query = $this->db->from('purchase_storage')->select('SUM(storage_number) AS today_storage_number')->where("supp_id = '{$row['suppliers_id']}' AND add_time >= '$today_start' AND add_time <= '$today_end'")->get();
            $row += $today_storage_query->row_array();

            $suppliers_storage_list[$row['suppliers_id']] = $row;
        }
        return $suppliers_storage_list;
    }

    public function supp_num($param)
    {
        $this->db->from('suppliers');

        if ($param['supp_name']) {
            $this->db->where("suppliers_name LIKE '%{$param['supp_name']}%'");
        }

        return $this->db->count_all_results();
    }

    public function suppliers_info($param = array())
    {
        $query = $this->db->from('suppliers')->where($param)->get();

        return $query->row_array();
    }

    public function insert_goods_supp($data = array())
    {

        $time = time();

        $_data = array(
            'in_price' => floatval($data['in_price']),
            'cn_name' => strval($data['cn_name']),
            'supp_url' => $data['supp_url'],
            'goods_json' => $data['goods_json'],
            'supp_sn' => strval($data['supp_sn']),
            'add_time' => $time,
            'update_time' => $time,
            'shop_price' => $data['shop_price']
        );

        $this->db->insert('goods_supp', $_data);

        return $this->db->insert_id();
    }


    public function goods_supp_info($arr=[]){
        $res= $this->db->from("goods_supp")->where($arr)->get();
        return $res->row_array();
    }
    //拉黑
    public function suppliers_status($suppliers_id){
        $query = $this->db->from("suppliers")->select("status")->where(['suppliers_id'=>$suppliers_id])->get();
        $status = $query->row_array();
        if($status['status'] == 1){
            $val = 2;
        }else{
            $val = 1;
        }
        $this->db->where(['suppliers_id'=>$suppliers_id]);
        $res = $this->db->update('suppliers', ['status'=>$val]);
        if($res){
            return 1;
        }
    }

}
